package com.jt.common.filter;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

interface Filter1{
    boolean asdfgh();
}
interface Servlet1{
    void ghjghj();
}
class FilterChain1{
    private List<Filter1> filters1=new CopyOnWriteArrayList<>();
    private Servlet1 servlet1;

    public FilterChain1(List<Filter1> filters1, Servlet1 servlet1) {
        this.filters1.addAll(filters1);
        this.servlet1 = servlet1;
    }

    //设置过滤链
    public void doFilter1(){
        for (int i = 0; i <filters1.size() ; i++) {
            if (!filters1.get(i).asdfgh()) {
                return;
            }
        }
        servlet1.ghjghj();
        for (int i = filters1.size()-1; i>=0 ; i--) {
            if (!filters1.get(i).asdfgh()) {
                return;
            }
        }
    }
}
public class XieBuRouFilter {
    public static void main(String[] args) {
        List<Filter1> filter1List=new CopyOnWriteArrayList<>();
        Filter1 filter11=new Filter1() {
            @Override
            public boolean asdfgh() {
                System.out.println("01111");
                return true;
            }
        };
        Filter1 filter21=new Filter1() {
            @Override
            public boolean asdfgh() {
                System.out.println("02222");
                return true;
            }
        };
        filter1List.add(filter11);
        filter1List.add(filter21);
        Servlet1 servlet1=new Servlet1() {
            @Override
            public void ghjghj() {
                System.out.println("蟹不肉");
            }
        };

        FilterChain1 fc1=new FilterChain1(filter1List, servlet1);
        fc1.doFilter1();

    }
}
